package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.model.Admin;
import com.xy.model.DeliverToStudent;
import com.xy.model.PackageBook;
import com.xy.model.Student;
import com.xy.service.DeliverToStudentService;
import com.xy.service.impl.DeliverToStudentServiceImpl;
import com.xy.service.impl.PackageBookServiceImpl;
import com.xy.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class DeliverToStudentServlet
 */
@WebServlet("/deliverToStudentServlet")
public class DeliverToStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeliverToStudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DeliverToStudentService service = new DeliverToStudentServiceImpl();
		
		if("add".equals(request.getParameter("status"))) {
			String [] choice = request.getParameterValues("checkbox");
			PackageBook pb = (PackageBook) new PackageBookServiceImpl().get(Integer.valueOf(request.getParameter("pb")));
			for(int i=0;i<choice.length;i++) {
				DeliverToStudent dts = new DeliverToStudent();
				dts.setAdmin((Admin) request.getSession().getAttribute("admin"));
				dts.setCreateDate(new Date());
				dts.setDeliverTime(new Date());
				dts.setStudent((Student) new StudentServiceImpl().get(Integer.valueOf(choice[i])));
				dts.setPackageBook(pb);
				service.add(dts);
			}
			request.getRequestDispatcher("/pages/public/main.jsp").forward(request, response);
		}else if("list".equals(request.getParameter("status"))) {
			request.setAttribute("dtss", service.list(DeliverToStudent.class));
			request.getRequestDispatcher("/pages/storage/storage001/student/deliver_query.jsp").forward(request, response);
		}else if("query".equals(request.getParameter("status"))) {
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			String hql = "from DeliverToStudent where deliverTime>='"+start+"'"
					+" and deliverTime<='"+end+"'";
			List<DeliverToStudent> dtss = new DeliverToStudentServiceImpl().queryByHql(hql);
			request.setAttribute("dtss", dtss);
			request.getRequestDispatcher("/pages/storage/storage001/student/deliver_query.jsp").forward(request, response);
		}
	}

}
